import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { $t } from '#/locales';

import {
  CommonStatusEnum,
  DICT_TYPE,
  SystemUserSocialTypeEnum,
} from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: $t('views.system.social.client.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.social.client.namePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'socialType',
      label: $t('views.system.social.client.socialType'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.SYSTEM_SOCIAL_TYPE, 'number'),
      },
      rules: 'required',
    },
    {
      fieldName: 'userType',
      label: $t('views.system.social.client.userType'),
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.USER_TYPE, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: 'required',
    },
    {
      fieldName: 'clientId',
      label: $t('views.system.social.client.clientId'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.social.client.clientIdPlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'clientSecret',
      label: $t('views.system.social.client.clientSecret'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.social.client.clientSecretPlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'agentId',
      label: 'agentId',
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.social.client.agentIdPlaceholder'),
      },
      dependencies: {
        triggerFields: ['socialType'],
        show: (values) =>
          values.socialType === SystemUserSocialTypeEnum.WECHAT_ENTERPRISE.type,
      },
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(CommonStatusEnum.ENABLE),
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: $t('views.system.social.client.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.social.client.namePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'socialType',
      label: $t('views.system.social.client.socialType'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.SYSTEM_SOCIAL_TYPE, 'number'),
        placeholder: $t('views.system.social.client.socialTypePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'userType',
      label: $t('views.system.social.client.userType'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.USER_TYPE, 'number'),
        placeholder: $t('views.system.social.client.userTypePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'clientId',
      label: $t('views.system.social.client.clientId'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.social.client.clientIdPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: $t('common.statusPlaceholder'),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: $t('views.common.id'),
      minWidth: 100,
    },
    {
      field: 'name',
      title: $t('views.system.social.client.name'),
      minWidth: 120,
    },
    {
      field: 'socialType',
      title: $t('views.system.social.client.socialType'),
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.SYSTEM_SOCIAL_TYPE },
      },
    },
    {
      field: 'userType',
      title: $t('views.system.social.client.userType'),
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.USER_TYPE },
      },
    },
    {
      field: 'clientId',
      title: $t('views.system.social.client.clientId'),
      minWidth: 180,
    },
    {
      field: 'status',
      title: $t('common.status'),
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'createTime',
      title: $t('common.createTime'),
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      title: $t('common.action'),
      width: 220,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
